package com.thenie.controller.admin;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thenie.pojo.Employee;
import com.thenie.pojo.Menu;
import com.thenie.service.MenuService;
import com.thenie.utils.*;
import com.thenie.vo.MenuVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 加载菜单（层级）
     * @param httpSession
     * @return
     */
    @RequestMapping("/loadMenu")
    public String loadMenu(HttpSession httpSession){
        //保存所有信息
        Map<String,Object> map=new HashMap<String, Object>();
        //创建map数组，保存homeInfo信息
        Map<String,Object> homeInfo =new HashMap<String, Object>();
        //创建map数组，保存logoInfo信息
        Map<String,Object> logoInfo =new HashMap<String, Object>();

        Employee employee = (Employee)httpSession.getAttribute(SystemConstant.LOGINUSER);
        //        //查询所有菜单列表
        //        List<Menu> menuList = menuService.findAllMenuList();
        List<Menu> menuList = menuService.findMenuListByEmpId(employee.getId());
        //中转站
        List<MenuNode> menuNodeList =new ArrayList<MenuNode>();
        for (Menu menu : menuList) {
            //创建菜单节点对象
            MenuNode menuNode = new MenuNode();
            menuNode.setHref(menu.getHref());//链接地址
            menuNode.setIcon(menu.getIcon());//菜单图标
            menuNode.setId(menu.getId());//菜单编号
            menuNode.setPid(menu.getPid());//父级菜单编号
            menuNode.setSpread(menu.getSpread());//是否展开
            menuNode.setTarget(menu.getTarget());//打开方式
            menuNode.setTitle(menu.getTitle());//菜单名称
            //将对象添加到集合
            menuNodeList.add(menuNode);
        }
        //保存HomeInfo信息
        homeInfo.put("title","首页");
        homeInfo.put("href","/admin/desktop");
        //保存logoInfo信息
        logoInfo.put("title","酒店管理系统");//logo标题
        logoInfo.put("image","/statics/layui/images/logo.png");//logo图片
        logoInfo.put("href","/admin/home.html");//首页地址

        map.put("homeInfo", homeInfo);
        map.put("logoInfo", logoInfo);
        map.put("menuInfo", MenuTreeUtil.toTree(menuNodeList, 0));


        return JSON.toJSONString(map);
    }

    /**
     * 加载左侧菜单树
     * @return
     */
    @RequestMapping("/loadMenuTree")
    public DataGridViewResult loadMenuTree(){
        List<Menu> menuList = menuService.findAllMenuList();
        //创建list集合  保存结点信息
        List<TreeNode> menuNodeList= new ArrayList<TreeNode>();
        for (Menu menu : menuList) {
            Boolean spread=(menu.getSpread()==null||menu.getSpread()==1)?true:false;

            menuNodeList.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), spread));
         }

        return new DataGridViewResult(menuNodeList);

    }

    /**
     * 查询菜单列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(MenuVo menuVo){
        //设置分页
        PageHelper.startPage(menuVo.getPage(),menuVo.getLimit());
        //查询菜单列表
        List<Menu> menuList = menuService.findMenuListByPage(menuVo);
        //创建分页对象
        PageInfo<Menu> menuPageInfo = new PageInfo<Menu>(menuList);
        //返回对应的数据格式
        return new DataGridViewResult(menuPageInfo.getTotal(), menuPageInfo.getList());
    }

    /**
     * 修改菜单数据
     * @param menu
     * @return
     */
    @RequestMapping("/updateMenu")
    public String updateMenu(Menu menu){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = menuService.updateMenu(menu);
        if(i>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @RequestMapping("/deleteMenu")
    public String deleteMenu(int menuId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = menuService.deleteMenu(menuId);
        if(i>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }
}
